<?php

class brands{
	function getBrandsList(){
		$brandList = array();
		qdbconn();
		
		$query = "SELECT * FROM brands
				  ";
		$result = mysql_query($query);
		
		$i = 0;
		while ($row = mysql_fetch_row($result)) {
			$brandList[$i]['br_id']		= $row[0];
			$brandList[$i]['br_name']	= $row[1];
			$brandList[$i]['br_status']	= $row[2];
			$i++;
		}
		return $brandList;
	}
	
	function inactivateABrand($br_id){
		qdbconn();		
		$query = "UPDATE brands SET br_status = 0 where br_id = $br_id";
		$result = mysql_query($query);
		return $result;
	}
	
	function activateABrand($br_id){
		qdbconn();		
		$query = "UPDATE brands SET br_status = 1 where br_id = $br_id";
		$result = mysql_query($query);
		return $result;
	}
	
	function editABrand($data){
		qdbconn();
		$br_name 	= $data['br_name'];
		$br_id 		= $data['br_id'];
		
		$query = "SELECT count(1) FROM brands
					WHERE br_name = '$br_name'";
		$result = mysql_query($query);
		if ($row = mysql_fetch_row($result)) {
			if ($row[0]==0) //br_name khong bi trung
			{
				$query = "UPDATE brands SET br_name='$br_name'
							WHERE br_id = $br_id";
				
				$result = mysql_query($query);
				return $result;
			}
			else 
			{
				return -1; //duplicated
			}
		}			
	}
	
	function insertABrand($data){
		qdbconn();
		$br_name = $data['br_name'];
		$query = "SELECT count(1) FROM brands
					WHERE br_name = '$br_name'";
		$result = mysql_query($query);
		if ($row = mysql_fetch_row($result)) {
			if ($row[0]==0) //br_name khong bi trung
			{
				$query = "INSERT INTO brands (br_name) values ('$br_name')";
				$result = mysql_query($query);
				return $result;
			}
			else 
			{
				return -1; //duplicated
			}
		}		
	}
	
	
	
	function getBrandsInfo($br_id){
		qdbconn();		
		$query = "SELECT * FROM brands
					WHERE br_id = '$br_id'";
		$result = mysql_query($query);
		$current_data = array();
		if ($row = mysql_fetch_row($result)) {
			$current_data['br_id'] 		= $row[0];
			$current_data['br_name'] 	= $row[1];
		}
		return $current_data;
	}
	
	
	
	
}

?>